package com.bocai.kelalacar.ui.car.set.ada;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bocai.kelalacar.R;
import com.bocai.kelalacar.util.BitmapUtils;
import com.bocai.kelalacar.util.DensityUtil;
import com.bocai.kelalacar.util.ScreenUtils;
import com.bocai.kelalacar.widget.PhotoRemoveIndex;
import com.bocai.kelalacar.widget.RoundedImageView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/23.
 */

public class PhotoAda extends BaseAdapter {
    private Context context;
    private ArrayList<String> photos = new ArrayList<>();
    private PhotoRemoveIndex photoRemoveIndex;
    public PhotoAda(Context context, ArrayList<String> photos) {
        this.context = context;
        this.photos = photos;
    }

    public void setPhotoRemoveIndex(PhotoRemoveIndex photoRemoveIndex){
        this.photoRemoveIndex = photoRemoveIndex;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (photos.size() < 6) {
            count = photos.size() + 1;
        } else {
            count = photos.size();
        }
        return count;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_photo, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (photos.size()!=6){

            if (photos.size()==0){
                viewHolder.llDeletePhoto.setVisibility(View.GONE);
//                viewHolder.ivPhoto.setImageResource(R.mipmap.add_img1);
                Glide.with(context).load(R.mipmap.add_img1).into(viewHolder.ivPhoto1);
                viewHolder.ivPhoto.setVisibility(View.GONE);
                viewHolder.ivPhoto1.setVisibility(View.VISIBLE);
//                viewHolder.ivPhoto.setImageDrawable(context.getResources().getDrawable(R.mipmap.add_img1));
            }else{
                if (position==photos.size()){
                    viewHolder.llDeletePhoto.setVisibility(View.GONE);
//                    viewHolder.ivPhoto.setImageDrawable(context.getResources().getDrawable(R.mipmap.add_img1));
                    Glide.with(context).load(R.mipmap.add_img1).into(viewHolder.ivPhoto1);
                    viewHolder.ivPhoto.setVisibility(View.GONE);
                    viewHolder.ivPhoto1.setVisibility(View.VISIBLE);

                }else{
                    viewHolder.ivPhoto.setVisibility(View.VISIBLE);
                    viewHolder.ivPhoto1.setVisibility(View.GONE);
                    viewHolder.llDeletePhoto.setVisibility(View.VISIBLE);
                    Glide.with(context).load(photos.get(position)).into(viewHolder.ivPhoto);
//                    viewHolder.ivPhoto.setImageBitmap(BitmapUtils.compressBitmap(photos.get(position)));
                    viewHolder.llDeletePhoto.setOnClickListener(new MyOnclickListener(position));
                }

            }

        }else{
            viewHolder.ivPhoto.setVisibility(View.VISIBLE);
            viewHolder.ivPhoto1.setVisibility(View.GONE);
            viewHolder.llDeletePhoto.setVisibility(View.VISIBLE);
            Glide.with(context).load(photos.get(position)).into(viewHolder.ivPhoto);
            viewHolder.llDeletePhoto.setOnClickListener(new MyOnclickListener(position));
        }
        setSize(viewHolder.ivPhoto);
        return convertView;
    }

    public class MyOnclickListener implements View.OnClickListener {
        private int position;

        public MyOnclickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (photoRemoveIndex!=null){
                photoRemoveIndex.remove(position);
            }
            photos.remove(position);
            notifyDataSetChanged();
        }
    }
    static class ViewHolder {
        @BindView(R.id.iv_photo)
        RoundedImageView ivPhoto;
        @BindView(R.id.iv_photo1)
        ImageView ivPhoto1;
        @BindView(R.id.ll_delete_photo)
        LinearLayout llDeletePhoto;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setPhotos(ArrayList<String> photos){
        this.photos = photos;
        notifyDataSetChanged();
    }


    public ArrayList<String> getPhotos(){
        return photos;
    }

    public void setSize(ImageView imageView){
        FrameLayout.LayoutParams para1;
        para1 = (FrameLayout.LayoutParams) imageView.getLayoutParams();
        para1.width = (ScreenUtils.getScreenWidth(context) - DensityUtil.dip2px(context,30))/3;
        para1.height = para1.width;
        imageView.setLayoutParams(para1);
    }

}
